package org.dromara.system.controller.system;

import java.util.List;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.bo.SysAgentBo;
import org.dromara.system.domain.query.SysAgentQuery;
import org.dromara.system.domain.vo.SysAgentVo;
import org.dromara.system.service.ISysAgentService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 代理商
 *
 * @author yixiacoco
 * @date 2025-09-03
 */
@Validated
@RestController
@RequestMapping("/system/agent")
public class SysAgentController extends BaseController {

    @Autowired
    private ISysAgentService sysAgentService;

    /**
     * 查询代理商列表
     */
    @SaCheckPermission("system:agent:list")
    @GetMapping("/list")
    public TableDataInfo<SysAgentVo> list(SysAgentQuery query) {
        return sysAgentService.queryPageList(query);
    }

    /**
     * 导出代理商列表
     */
    @SaCheckPermission("system:agent:export")
    @Log(title = "代理商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysAgentQuery query, HttpServletResponse response) {
        List<SysAgentVo> list = sysAgentService.queryList(query);
        ExcelUtil.exportExcel(list, "代理商", SysAgentVo.class, response);
    }

    /**
     * 获取代理商详细信息
     *
     * @param agentId 主键
     */
    @SaCheckPermission(value = {"system:agent:query", "system:agent:edit"}, mode = SaMode.OR)
    @GetMapping("/{agentId}")
    public R<SysAgentVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String agentId) {
        return R.ok(sysAgentService.queryById(agentId));
    }

    /**
     * 新增代理商
     */
    @SaCheckPermission("system:agent:add")
    @Log(title = "代理商", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysAgentBo bo) {
        return toAjax(sysAgentService.insertByBo(bo));
    }

    /**
     * 修改代理商
     */
    @SaCheckPermission("system:agent:edit")
    @Log(title = "代理商", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysAgentBo bo) {
        return toAjax(sysAgentService.updateByBo(bo));
    }

    /**
     * 删除代理商
     *
     * @param agentIds 主键串
     */
    @SaCheckPermission("system:agent:remove")
    @Log(title = "代理商", businessType = BusinessType.DELETE)
    @DeleteMapping("/{agentIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] agentIds) {
        return toAjax(sysAgentService.deleteWithValidByIds(List.of(agentIds)));
    }

    /**
     * 查询状态为启用且层级为1或2的渠道商列表
     *
     * @return 渠道商列表
     */
    @SaCheckPermission("system:agent:list")
    @GetMapping("/enabledAgents")
    public R<List<SysAgentVo>> queryEnabledAgents() {
        return R.ok(sysAgentService.queryEnabledAgents("1,2"));
    }
}
